class Solution {
public:
    int deleteAndEarn(vector<int>& nums)
    {
        vector<int> arr(100001);
        for (int i = 0; i < nums.size(); i++)
        {
            arr[nums[i]] += nums[i];
        }
        vector<int> f(100001), g(100001);

        for (int i = 1; i < 100001; i++)
        {
            f[i] = g[i - 1] + arr[i];
            g[i] = max(f[i - 1], g[i - 1]);
        }
        return max(f[100000], g[100000]);
    }
};